<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * This is the key class to all the admin side data nodes.
    * It is extended into child classes which do all the work.
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * Class adminNodePageadv
    ****
    * This class handles the editing of advanced HTML pages in INA
    ****
    * @package adminNodes
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    ***/
    class adminNodeShopArticle extends adminNode{

		/**
         * a data processor.
         *
         * @return
         **/
        function process(){
            if($_POST){

                //upload picture (if any)
                if($_POST['img'] or $this->uploadPicture()){
                    $img = "TRUE";
                }else{
                    $img = "FALSE";
                }

                //check supplier
                if($_POST['supplier'] == 'New'){
                    $this->db->Execute("INSERT INTO shop_suppliers(name) VALUES(".$this->db->qstr($_POST['supplier_name']).")");
                    $_POST['supplier'] = $this->db->getOne("SELECT last_value FROM shop_suppliers_id_seq");
                }elseif((int)$_POST['supplier'] == 0){
                    $_POST['supplier'] = 'NULL';
                }else{
                    $_POST['supplier'] = (int)$_POST['supplier'];
                }

                if($_POST['on_splash'] == 't'){
                    $onSplash = 'TRUE';
                }else{
                    $onSplash = 'FALSE';
                }

                if($_POST['on_package'] == 't'){
                    $onPackage = 'TRUE';
                }else{
                    $onPackage = 'FALSE';
                }

                //update database record
                $this->db->Execute("UPDATE
                                        node_shop_article SET
                                            image        = $img,
                                            teaser       = ".$this->db->qstr($_POST['teaser']).",

                                            teaser_title = ".$this->db->qstr($_POST['teaser_title']).",
                                            banner_title = ".$this->db->qstr($_POST['banner_title']).",
                                            banner_desc  = ".$this->db->qstr($_POST['banner_desc']).",

                                            ticket_title = ".$this->db->qstr($_POST['ticket_title']).",
                                            ticket_text  = ".$this->db->qstr($_POST['ticket_text']).",
                                            ticket_text2 = ".$this->db->qstr($_POST['ticket_text2']).",

                                            adr_1        = ".$this->db->qstr($_POST['adr_1']).",
                                            adr_2        = ".$this->db->qstr($_POST['adr_2']).",
                                            adr_city     = ".$this->db->qstr($_POST['adr_city']).",
                                            adr_country  = ".$this->db->qstr($_POST['adr_country']).",
                                            adr_zip      = ".$this->db->qstr($_POST['adr_zip']).",

                                            gmaps_long   = ".$this->db->qstr($_POST['gmaps_long']).",
                                            gmaps_lat    = ".$this->db->qstr($_POST['gmaps_lat']).",

                                            on_splash    = ".$onSplash.",
                                            on_package   = ".$onPackage.",

                                            content      = ".$this->db->qstr($_POST['content']).",
                                            price        = ".(float)$_POST['base_price'].",
                                            price_kids   = ".(float)$_POST['discount_price'].",
                                            tax          = ".(float)round($_POST['tax']/100,2).",
                                            kid_age	 = ".(int)$_POST['kid_age'].",

                                            supplier     = ".$_POST['supplier'].",
                                            supplier_aid = ".$this->db->qstr($_POST['supplier_aid'])."
                                        WHERE id = " . $this->object);

                //updates the status of the page (title, wf state)
                $this->updatePageStatus();

                //make a note of the ajax request
                $this->log->recordAction("Updated Shop Article (".$this->object.") with global id (".$this->pageref.")");

		$this->pushSearchIndex();

                header('Location: '.$this->request.'&confirm=true');
                exit;
            }else{
                $this->smarty->assign('page',$this->db->getRow("SELECT
                                                                    node_shop_article.*,
                                                                    pagemap.title
                                                                FROM  pagemap LEFT JOIN node_shop_article ON pagemap.pageid = node_shop_article.id
                                                                WHERE pagemap.id = " . $this->pageref));

                $this->smarty->assign('suppliers',$this->db->getAssoc("SELECT id, name FROM shop_suppliers ORDER BY name"));
            }
        }

        /**
         * Will set the HTML template needed for
         * the child's operation
         *
         * @return
         **/
        function setTemplate(){
            $this->setActionTemplate('nodes/editShopArticle.html');
        }


        /**
         * Update the search index for the item
         *
         * @return void
         **/
        private function pushSearchIndex(){
            //create search index records
            $mySearch = new searchIndex($this->db,$this->log);
            $mySearch->pushToIndex($this->pageref,$this->db->getOne("SELECT content FROM node_shop_article WHERE id = " . $this->object));

            //$this->log->recordAction("Created Search Index for Article (".$this->object.") with global id (".$this->pageref.")");


            //do the magic
            /*
            $pages = $this->db->getAll("SELECT id AS ref, pageid AS obj FROM pagemap WHERE pagetype = '101'");
            foreach($pages as $p){
                echo $p['obj'].'<br />';
                $mySearch->pushToIndex($p['ref'],$this->db->getOne("SELECT content FROM node_shop_article WHERE id = " . $p['obj']));
            }
            */

        }

        /**
         * method which will handle a default add action to the
         * dedicated table
         *
         * @return integer ID of new object
         * @see ajaxAddPage
         * @see ajaxAddNode
         **/
        function addInstance(){
            $this->db->Execute("INSERT INTO node_shop_article(content) VALUES('')");
            return $this->db->getOne("SELECT last_value FROM node_shop_article_id_seq");
        }
    }
